/*
 * ========================================================
 * Copyright(c) 2012 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.hzdracom.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.data.db2.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.simon.mengkou.data.db2.table.UserTable.UserColumns;
import com.simon.mengkou.data.bean.base.User;
import com.simon.mengkou.data.db2.OuerDBHelper;
import com.simon.mengkou.data.db2.table.UserTable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Zhenshui.Xia
 * @date   : 2014-3-28
 * @desc   : 用户表业务逻辑
 */

public class UserDao {
	private OuerDBHelper mOpenHelper;

    public UserDao(Context context) {
    	mOpenHelper = new OuerDBHelper(context);
    }


	/**
	 * 添加用户入库
	 * @param user
	 */
	public void addUser(User user) {
		if(user != null) {
			SQLiteDatabase db = mOpenHelper.getWritableDatabase();

			ContentValues values = new ContentValues();
			values.put(UserColumns.USERID,    		user.getUserId());
			values.put(UserColumns.TYPE,    		user.getType());
			values.put(UserColumns.NICK,    		user.getNick());
			values.put(UserColumns.AVATAR,    		user.getAvatarUrl());

			Cursor cursor = null;

			try {
				cursor = db.query(UserTable.TABLE, null, UserColumns.USERID +"=?",
						new String[]{user.getUserId()}, null, null, null);

				if(cursor != null && cursor.getCount()>0) {
					db.update(UserTable.TABLE, values, UserColumns.USERID +"=?", new String[]{user.getUserId()});
				} else {
					db.insert(UserTable.TABLE, null, values);
				}
			} catch(Exception ex) {
				ex.printStackTrace();
			} finally {
				if(cursor != null) cursor.close();
			}

			db.close();
		}
	}

    /**
     * 获取用户信息
     * @return
     */
    public User getUser(String uid) {
    	SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		User user = null;
		Cursor cursor = null;
		
		try {
			cursor = db.query(UserTable.TABLE, new String[]{
					UserColumns.USERID,
					UserColumns.TYPE,
					UserColumns.NICK,
					UserColumns.AVATAR},
					UserColumns.USERID +"=?", new String[]{uid}, null, null, null);
			
			if(cursor != null && cursor.moveToNext()) {
				user = new User();
				user.setUserId(uid);
				user.setType(cursor.getType(1));
				user.setNick(cursor.getString(2));
				user.setAvatarUrl(cursor.getString(3));
			}
		} catch(Exception ex) {
			user = null;
			ex.printStackTrace();
		} finally {
			if(cursor != null) cursor.close();
		}
		
		db.close();
		return user;
    }

	/**
	 * 获取所有用户
	 * @return
	 */
	public List<User> getUsers(){
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		List<User> users = null;
		Cursor cursor = null;

		try {
			cursor = db.query(UserTable.TABLE, new String[]{
							UserColumns.USERID,
							UserColumns.TYPE,
							UserColumns.NICK,
							UserColumns.AVATAR}, null, null, null, null, null);

			if(cursor != null) {
				users = new ArrayList<>();

				while (cursor.moveToNext()) {
					User user = new User();
					user.setUserId(cursor.getString(0));
					user.setType(cursor.getType(1));
					user.setNick(cursor.getString(2));
					user.setAvatarUrl(cursor.getString(3));
					users.add(user);
				}
			}
		} catch(Exception ex) {
			users = null;
			ex.printStackTrace();
		} finally {
			if(cursor != null) cursor.close();
		}

		db.close();

		return users;
	}

}
